package uplift.project;

import java.util.ArrayList;

import uplift.project.model.VolunteerEvent;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class BrowseAdapter extends ArrayAdapter<VolunteerEvent> {

	private ArrayList<VolunteerEvent> events;

	public BrowseAdapter(Context context, int textViewResourceId, ArrayList<VolunteerEvent> events) {
		super(context, textViewResourceId, events);
		this.events = events;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		if (v == null) {
			LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.event_list_item, null);
		}
		VolunteerEvent vEvent = events.get(position);
		if (vEvent != null) {
			TextView name = (TextView) v.findViewById(R.id.event_name);
			ImageView image = (ImageView) v.findViewById(R.id.event_image);
			TextView location = (TextView) v.findViewById(R.id.event_location);
			TextView date = (TextView) v.findViewById(R.id.event_date);
			TextView time = (TextView) v.findViewById(R.id.event_time);
			if (name != null) {
				name.setText(vEvent.getName());                            
			}
			if(image != null){
				// set image to match category
				String category = vEvent.getCategory();
				if(category.equals("Elderly")) {
					// gray
					image.setImageResource(R.drawable.logo_silver);
				} else if(category.equals("Food")) {
					// red
					image.setImageResource(R.drawable.logo_red);
				} else if(category.equals("Nature")) {
					// green
					image.setImageResource(R.drawable.logo_green);
				} else if(category.equals("Animals")) {
					// yellow
					image.setImageResource(R.drawable.logo_yellow);
				} else { // category.equals("misc")
					// blue
					image.setImageResource(R.drawable.logo_blue);
				}
			}
			if(location != null) {
				location.setText(vEvent.getLocation());
			}
			if(date != null) {
				date.setText(vEvent.getDate());
			}
			if(time != null) {
				time.setText(vEvent.getStartTime() + " - " + vEvent.getEndTime());
			}
		}
		return v;
	}
}
